#include <iostream>
#include <string>
#include <math.h>
#include <fstream>
#include <algorithm>
#include <stdio.h>
#include <boost/filesystem/path.hpp>
#include <boost/program_options.hpp>
#include "get_arg.h"



// link with -lboost_filesystem-gcc -lboost_program_options-gcc
// program_options_helper.cpp
//-lboost_filesystem-gcc-d
//-L/usr/local/lib





// my functions
#include <print_error.h>
#include <program_options_helper.h>
#include <stlhelper.h>
#define DEBUG_LEVEL 6
#include <debug_func.h>

using std::string;
using std::cerr;
using std::string;
using std::ofstream;
using std::ifstream;
using std::cerr;
using std::cout;
using std::max;
using boost::filesystem::path;
#define DEFAULT_CODEML_PATH "/net/cpp-group/tools/paml/"
using namespace program_options_helper;
namespace po = boost::program_options_lg;

//_______________________________________________________________________________________

//		process_command_line_options

//				from command line
//				Created [9/13/2001]

//_______________________________________________________________________________________
bool process_command_line_options(int argc, char* argv[], t_program_args& args)
{

	//
	// checked mandatory arguments
	//
	po::options_description mandatory_args("Mandatory Arguments");
	mandatory_args.add_options()
	("matching_identifiers_set1,m",	
							po::value<string>(&args.file_identifiers1)
								->set_name("FILE"),
							"First list of identifiers to match against the those in the "
							"sequence pairs results.")
	("matching_identifiers_set2,n",	
							po::value<string>(&args.file_identifiers2)
								->set_name("FILE"),
							"Second list of identifiers to match against the those in the "
							"sequence pairs results.");

	args.optional_args.add_options()
	("input_file,i", 	po::value<string>(&args.file_input)
								->set_name("FILE")
								->default_value("STDIN"),
							"Sequence pairs results to be filtered.")
	("ids_only,I",		po::bool_switch(&args.ids_only), 	
							"Only print out the identifiers.")
	("different,D",		 	po::value<string>(&args.file_different)
								  ->set_name("FILE"),
							"Sequence pairs results where both identifiers "
							"belong to different --matching_identifiers_sets.\n"
							"This holds even if an identifier also belongs to both sets.")
	("same,S",    	po::value<string>(&args.file_same)
								->set_name("FILE"),
							"Sequence pairs results where both identifiers "
							"belong to the same --matching_identifiers_set.\n"
							"This holds even if an identifier also belongs to both sets.")
	("neither,N", 		po::value<string>(&args.file_neither)
								 ->set_name("FILE"),
							"Sequence pairs results where one or other of the identifiers "
							"do not both match either of the --matching_identifiers_sets.\n")
	("#comment,#",		po::value<string>(&args.comment_letters)
								->set_name("LETTERS")
								->default_value("#"),
							"Use one of these letters to begin a "
							"commented line.");


	args.use_results_summary();
	args.add_std_options();
	po::options_description all_args("");
	all_args.add(mandatory_args).add(args.optional_args);

	// parse command line
	po::variables_map vm;
	po::store(po::command_line_parser(argc, argv).options(all_args).run(), vm);
	po::notify(vm);

	if (vm.count("version"))
	{
		print_version(cerr, *argv, "1.6\n"
			"\tHistory: \n\n"
			"\tv 1.0"
			"1-10-2007\1"
			"Derived from seq_pairs_filter_by_identifier\n"
			);
		return false;
	}
	if (vm.count("help"))
	{
		string exe_description =
					"Takes sequence identifier result pairs and prints out lines "
					"which match those in --matching_identifiers_set1 or "
					"--matching_identifiers_set2.\n";

		po::options_description visible_args("");
		visible_args.add(mandatory_args).add(args.optional_args);
		print_usage(cerr, string(*argv),	"--matching_identifiers_set1 FILE\n"
											"--matching_identifiers_set2 FILE\n"
											"--input_file SEQ_PAIR.RESULTS\n"
											"[OPTIONS]\n",
							visible_args, exe_description, 80);
		return false;
	}
	check_required_options(vm, mandatory_args);

	// remove empty strings
	std::vector<string> file_names;
	file_names.push_back(args.file_different   );
	file_names.push_back(args.file_same  );
	file_names.push_back(args.file_neither);
	file_names.erase(std::remove(file_names.begin(), file_names.end(), string()),
					 file_names.end());

	// Are there any duplicates
	std::sort(file_names.begin(), file_names.end());
	std::vector<string>::iterator i = std::unique(file_names.begin(), file_names.end());
	if (i != file_names.end())
		throw std::logic_error("Duplicated filename: [" + *i + "]");
	
	
	
	

	args.open_err_log();
	return true;
}













